﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ISmart.Interface;
using System.Xml;

namespace ISmart
{
    public class GetUserList
    {
        List<ISmart.Models.UserItem> items;

        /// <summary>
        /// 获取用户列表
        /// </summary>
        public List<ISmart.Models.UserItem> Items
        {
            get
            {
                return items;
            }
        }
        
        public GetUserList(string asspUserId, string keyCode, int currentPage, int pageSize, bool isAccurate, string realName, WeeBool state)
        {
            InitClass(asspUserId, keyCode, currentPage, pageSize, isAccurate, realName, state);
        }

        public GetUserList(string asspUserId, string keyCode)
        {
            InitClass(asspUserId, keyCode, Config.InitCurrentPage, Config.InitPageSize, false, "", WeeBool.Null);
        }

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="asspUserId">API用户ID</param>
        /// <param name="keyCode">验证密钥</param>
        /// <param name="currentPage">当前页</param>
        /// <param name="pageSize">页面大小</param>
        /// <param name="isAccurate">是否精确查询</param>
        /// <param name="userName">用户名</param>
        /// <param name="state">当前状态</param>
        private void InitClass(string asspUserId, string keyCode, int currentPage, int pageSize, bool isAccurate, string realName, WeeBool state)
        {
            KeyValue[] kv = {
                                new KeyValue("asspUserId", asspUserId),
                                new KeyValue("keyCode", keyCode),
                                new KeyValue("currentPage", currentPage.ToString()),
                                new KeyValue("pageSize", pageSize.ToString()),
                                new KeyValue("isAccurate", isAccurate.ToString()),
                                new KeyValue("realName", realName),
                                new KeyValue("state", ((int)state).ToString()),
                            };
            AnalysisXml ax = new AnalysisXml();
            items = new List<Models.UserItem>();

            ax.GetXmlDoc(Config.ApiUrl + "Users.asmx/GetUserList", kv);
            XmlNodeList xnl = ax.GetXmlNodeList("UserItem");

            foreach (XmlNode xn in xnl)
            {
                items.Add(new Models.UserItem()
                {
                    UID = Convert.ToInt32(ax.GetXmlNodeText(xn, "UID")),
                    UserName = ax.GetXmlNodeText(xn, "UserName"),
                    RealName = ax.GetXmlNodeText(xn, "RealName"),
                    Tel = ax.GetXmlNodeText(xn, "Tel"),
                    Email = ax.GetXmlNodeText(xn, "Email"),
                    QQ = ax.GetXmlNodeText(xn, "QQ"),
                    State = ax.GetXmlNodeText(xn, "State")
                });
            }
        }
    }
}
